package com.taomu.app.zongchouhuli.utils.zxing;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class CreateQrHelper {
    public CreateQrHelper() {
    }

    public static Bitmap createBitmap(String content, int size) throws WriterException {
        HashMap<EncodeHintType, Object> hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        BitMatrix bitMatrix = (new QRCodeWriter()).encode(content, BarcodeFormat.QR_CODE, size, size, hints);
        int[] piexls = new int[bitMatrix.getWidth() * bitMatrix.getHeight()];

        for(int h = 0; h < bitMatrix.getHeight(); ++h) {
            int offset = h * bitMatrix.getWidth();

            for(int w = 0; w < bitMatrix.getWidth(); ++w) {
                piexls[offset + w] = bitMatrix.get(w, h) ? -16777216 : -1;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(bitMatrix.getWidth(), bitMatrix.getHeight(), Bitmap.Config.ARGB_8888);
        bitmap.setPixels(piexls, 0, size, 0, 0, size, size);
        return bitmap;
    }

    public static Bitmap BarcodeFormatCode(String str) {
        int width = 850;
        int height = 160;

        BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
        BitMatrix matrix = null;
        try {
            str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
            matrix = new MultiFormatWriter().encode(str, barcodeFormat, width, height, null);
            return bitMatrix2Bitmap(matrix);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
        int w = matrix.getWidth();
        int h = matrix.getHeight();
        int[] rawData = new int[w * h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                int color = Color.WHITE;
                if (matrix.get(i, j)) {
                    // 有内容的部分，颜色设置为黑色，可以自己修改成其他颜色
                    color = Color.BLACK;
                }
                rawData[i + (j * w)] = color;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
        return bitmap;
    }

    private void createBarCode(){

    }
}
